<html>
<head><meta charset="utf-8"><title>mem::zeroed unconditional panic · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html">mem::zeroed unconditional panic</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="231254494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231254494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231254494">(Mar 22 2021 at 01:41)</a>:</h4>
<p>Maybe I'm missing something, but why do we have intrinsics that statically either unconditionally panic or noop (<a href="https://github.com/rust-lang/rust/issues/66059">#66059</a>)? Why not just emit a compiler error?</p>



<a name="231254563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231254563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231254563">(Mar 22 2021 at 01:43)</a>:</h4>
<p>it broke too much of the ecosystem I think <a href="https://github.com/rust-lang/rust/pull/66059#issuecomment-586734747">https://github.com/rust-lang/rust/pull/66059#issuecomment-586734747</a></p>



<a name="231254922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231254922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231254922">(Mar 22 2021 at 01:50)</a>:</h4>
<p>What would the difference in breakage be between a compiler error and an unconditional runtime panic?</p>



<a name="231254935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231254935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231254935">(Mar 22 2021 at 01:51)</a>:</h4>
<p>the runtime panic only breaks code if it's actually executed</p>



<a name="231254947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231254947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231254947">(Mar 22 2021 at 01:52)</a>:</h4>
<p>Hmm</p>



<a name="231254993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231254993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231254993">(Mar 22 2021 at 01:52)</a>:</h4>
<p>Where is the rationale explained? I didn't see anything about why a panic instead of a compiler error in that comment.</p>



<a name="231255007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231255007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231255007">(Mar 22 2021 at 01:52)</a>:</h4>
<p>not sure, sorry</p>



<a name="231255016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231255016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231255016">(Mar 22 2021 at 01:53)</a>:</h4>
<p>I think what confuses me is it says:</p>
<blockquote>
<p>We already have a lint warning against this where statically possible; I think it is time to also add some run-time checking to cover the cases the lint cannot find.</p>
<p>On the technical side, the UB check is implemented through an intrinsic that either compiles to an unconditional panic (if UB was detected) or a NOP (otherwise). </p>
</blockquote>
<p>It seems to say that the static lint can't always detect it, but that this PR was implemented via a static check...?</p>



<a name="231255789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231255789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231255789">(Mar 22 2021 at 02:06)</a>:</h4>
<p>It is not undefined behavior to <em>have</em> code that does this; it is only UB if that code is reached at execution time. For example, if the program conditionally does so, it may still be unsound (e.g., if the condition is insufficient to prevent UB from happening), but it may also be sound (if the condition is sufficient to not do so). So making a call to mem::zeroed unconditionally a compile error if it is used with a type that means executing that code would be UB would be wrong.</p>



<a name="231255894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231255894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231255894">(Mar 22 2021 at 02:08)</a>:</h4>
<p>Hmm, but what's the purpose of conditionally using <code>mem::zeroed</code> in an invalid way if when the condition is reached it will be UB? Are you saying that sometimes you can make it not UB?</p>



<a name="231258856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231258856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231258856">(Mar 22 2021 at 03:08)</a>:</h4>
<p>Sure, for example if the condition is only true for some safe types (e.g. imagine an  Any-based guard for integer types, and a more general code path for other types, or just a panic).</p>



<a name="231258916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/mem%3A%3Azeroed%20unconditional%20panic/near/231258916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/mem.3A.3Azeroed.20unconditional.20panic.html#231258916">(Mar 22 2021 at 03:09)</a>:</h4>
<p>Ah, I think I understand better. I always forget about <code>Any</code> <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>